package com.doeis.minaserver.bean;

/**
 * 用户状态
 * */
public class Presence extends UserBasic {	
	/**
	 * 离线状态
	 */
	public static final int OFFLINE = 0;
	/**
	 * 在线状态
	 */
	public static final int ONLINE = 100;	
	/**
	 * 隐身状态
	 */
	public static final int HIDEN = 101;
	/**
	 * 离开状态
	 */
	public static final int AWAY = 200;
	/**
	 * 忙碌状态
	 */
	public static final int BUSY = 300;	
	/**
	 * 请勿打扰
	 */
	public static final int DND = 400;		
	
	/**
	 * 在线状态
	 */
	private Integer value = ONLINE;
	
	/**
	 * 状态的描述
	 */
	private String desc ;
	
	
	/**
	 * 默认构造函数
	 */
	public Presence(){	
		this(ONLINE);
	}
	
	public Presence(int value){
		this(value, null);
	}
	
	public Presence(Integer value, String desc) {
		this(null, value, desc);
	}
	
	public Presence(String userid, Integer value, String desc) {
		super();
		this.userid = userid;
		this.value = value;
		this.desc = desc;
	}

	/**
     * @return the value
     */
    public Integer getValue(){
    	return value;
    }

	/**
	 * @return the desc
	 */
	public String getDesc(){
		return desc;
	}

	/**
     * @param value the value to set
     */
    public void setValue(Integer value){
    	this.value = value;
    }

	/**
     * @param desc the desc to set
     */
    public void setDesc(String desc){
    	this.desc = desc;
    }

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Presence [value=" + value + ", desc=" + desc + ", userid="
				+ userid + "]";
	}

	/**
	 * 把状态值转换为可以显示的字符串
	 * @param value
	 * @return
	 */
	public static String presenceDisplay(int value, boolean isOwner)
	{
		switch(value){
			case Presence.OFFLINE:	return isOwner?"离线":"隐身或离线";
	    	case Presence.ONLINE:	return "在线";
	    	case Presence.HIDEN: 	return "隐身或离线";
	    	case Presence.BUSY:		return "忙碌";
	    	case Presence.AWAY:		return "离开";
	    	case Presence.DND: 		return "勿扰";	    	
	    	default:	return "未知";
    	}
	}
	
	public static String presenceDisplay(Presence presence, boolean isOwner){
		if(presence!=null){
			String text = presence.getDesc();
			if(text == null || text.trim().length()==0){
				text = Presence.presenceDisplay(presence.getValue(), isOwner);
			}
			return text;
		}
		return presenceDisplay(0, isOwner);
	}

	/**
     * 检查是否是合法的presence
     * @param presence
     * @return
     */
    public static boolean isValidPresenceValue(int presence)
    {
    	switch(presence) {
    	case AWAY:
    	case ONLINE:
    	case HIDEN:
    	case BUSY:
    	case OFFLINE:
    	case DND:	
    		return true;
    	
    	default:
    		return false;    		
    	}
    }
}
